<?php
// **************************************************************
//  Copyright 2007 AMC
//  Glenview, Illinois
//
//  File:      member_edit.php
//  Author:    Mickey Freeman (mfreeman@connect2amc.com)
//  Function:  Page for registering a member
//
//  Created: 08/16/07
//  Version: 1.0
//
//  Modified:
// **************************************************************
require_once 'init.php';

include_once("../fckeditor/fckeditor.php") ;


$obj_page      = new pages();

$page_id = $_REQUEST[page_id];
// pulling the page task
$task = $_REQUEST['task'];
$obj = new ws_secure_user();
$obj->load($usr_id);


if (!$usr_id){
  $obj->usr_active = 1;
}
switch ($task) {
  case 'delete':

    $delete_id  = $_REQUEST['del_id'];
    $obj_page->delete($delete_id);

    header("Location:index.php");
    exit;
    break;

  case 'save': //saving a submitted registration form form
  $obj_page->load_from_request();

  //	$errors = $obj_page->validate_url();
  $errors  = array();

  if (count($errors) != 0){ // if there are errors then redisplay the form with the errors
    //  $title = $lang['Reg_title'];
    include ABSTRACT_PUBLIC_HEADER;


    // assigning variables to the template and displaying
    $page_data = array(
    'page'         => $obj_page->values,
    'errors'      => $errors,
    'cancel_link' => $_SERVER['PHP_SELF']);

    include 'templates/tpl_extract_url.php';

    include ABSTRACT_PUBLIC_FOOTER;

  } else { // if there are no errors then register the member and log them in

    $existing_url  = $_REQUEST['page_url1'];
    $sep_url       = explode(chr(10),$existing_url);

    if(is_array( $sep_url))
    {

      foreach($sep_url as $url)
      {
        if($url!='')
        {

          //$obj_read_file     = new read_url(trim($url));
          $obj_pagesave      = new pages();

          //  $homepage   = file_get_contents(trim($url));

          //$data     =  $obj_read_file->getFileContents();
          $curl_handle = curl_init();
          // Where should we get the data?
          curl_setopt ($curl_handle, CURLOPT_URL, trim($url));
          // This says not to dump it directly to the output stream, but instead
          // have it return as a string.
          curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
          // the following is optional, but you should consider setting it
          // anyway. It prevents your page from hanging if the remote site is
          // down.
          curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, 1);
          // Now, YOU make the call.
          $data  = curl_exec($curl_handle);
          // And tell it to shut down (when your done. You can always make more
          // calls if you want.)
          curl_close($curl_handle);
          // This is where i'd probably do some extra checks on what i just got.
          // Paranoia pays dividends.
          //print $buffer;




          // SUCCESS, DISPLAY FILE CONTENTS
          $data  = str_replace("/etc","etc",$data);
          $data  = str_replace("/images","../images",$data);
          $header = $obj_pagesave->ExtractString($data, '',$_REQUEST['page_start_flag1']);

          $body   = $obj_pagesave->ExtractString($data, $_REQUEST['page_start_flag1'], $_REQUEST['page_end_flag1']);

          $footer = $obj_pagesave->ExtractString($data, $_REQUEST['page_end_flag1'], '');

          $obj_pagesave->values['page_url']               = $url;
          $obj_pagesave->values['page_header']            = $header;
          $obj_pagesave->values['page_body']              = $body;
          $obj_pagesave->values['page_footer']            = $footer;
          $obj_pagesave->values['page_old_html']          = $data;
          $obj_pagesave->values['page_add_date']          = time();
          $obj_pagesave->values['page_last_modified']     = time();
          $obj_pagesave->values['page_status']            = 1;
          $obj_pagesave->values['page_start_flag']        = $_REQUEST['page_start_flag1'];
          $obj_pagesave->values['page_end_flag']          = $_REQUEST['page_end_flag1'];

          $obj_pagesave->save();
        }
      }
    }
    else
    {

      //$obj_read_file = new read_url($_REQUEST['page_url1']);
      $curl_handle = curl_init();
      // Where should we get the data?
      curl_setopt ($curl_handle, CURLOPT_URL, trim($_REQUEST['page_url1']));
      // This says not to dump it directly to the output stream, but instead
      // have it return as a string.
      curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
      // the following is optional, but you should consider setting it
      // anyway. It prevents your page from hanging if the remote site is
      // down.
      curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, 1);
      // Now, YOU make the call.
      $data  = curl_exec($curl_handle);
      // And tell it to shut down (when your done. You can always make more
      // calls if you want.)
      curl_close($curl_handle);
      // This is where i'd probably do some extra checks on what i just got.
      // Paranoia pays dividends.
      //print $buffer;

      //$data         =  $obj_read_file->getFileContents();

      // SUCCESS, DISPLAY FILE CONTENTS
      $data  = str_replace("/etc","etc",$data);
      $data  = str_replace("/images","../images",$data);
      $header = $obj_page->ExtractString($data, '',$_REQUEST['page_start_flag1']);

      $body   = $obj_page->ExtractString($data, $_REQUEST['page_start_flag1'], $_REQUEST['page_end_flag1']);

      $footer = $obj_page->ExtractString($data, $_REQUEST['page_end_flag1'], '');
      $obj_page->values['page_url']           = $url;
      $obj_page->values['page_header']            = $header;
      $obj_page->values['page_body']              = $body;
      $obj_page->values['page_footer']            = $footer;
      $obj_page->values['page_old_html']          = $data;
      $obj_page->values['page_add_date']          = time();
      $obj_page->values['page_last_modified']     = time();
      $obj_page->values['page_status']            = 1;
      $obj_page->values['page_start_flag']    = $_REQUEST['page_start_flag1'];
      $obj_page->values['page_end_flag']      = $_REQUEST['page_end_flag1'] ;

      $obj_page->save();


    }
    header("Location: " . APP_BASE_REL_PATH . "ws_joomla_import/");
    exit();

  }
  break;

  case 'add': // add url
  include ABSTRACT_PRIVATE_HEADER;
  include 'templates/tpl_extract_url.php';
  include ABSTRACT_PRIVATE_FOOTER;
  break;

  case 'edit': // edit url
  $editId    = $_REQUEST['editto'];
  $page_data = array('page'   => $obj_page->get_page_detail($editId) ,
  'page_id'=>  $editId
  );



  include ABSTRACT_PRIVATE_HEADER;
  include 'templates/tpl_edit_page.php';
  include ABSTRACT_PRIVATE_FOOTER;
  break;
  case 'update':

    $obj_page->load($page_id);
    $obj_page->update_from_request();
    //	  echo '<pre>';print_r($obj_page);echo '</pre>';die();
    $obj_page->save();
    header("Location: " . APP_BASE_REL_PATH . "ws_joomla_import/");
    exit();
    break;

  default:
    //global $page_title, $sec, $user_access_level, $security;

    $sql = "SELECT *,trim(page_url) as pu, 'Edit' as page_edit, 'Preview' as page_preview, 'Delete' as page_delete "
    . "FROM " . PAGE_TABLE . "  ";

    $obj_list = new app_list($sql, 'page_id', 'page_id', 'DESC', 'normal', 'normal', '0', '5', true, '25','','','','100%');

    $obj_list->sort_url = $_SERVER['PHP_SELF'];

    $obj_list->empty_message = '<span class="normal"> There are currently no pages in the system.</span>';
    $obj_list->add_column('page_id', 'Id ', '', '', '', '', true);
    $obj_list->add_column('pu', 'URL', '', '', '%%%pu%%%', '', true,'','text','','_BLANK');
    $obj_list->add_column('page_preview', '', '', '', '#', 'popwin5("preview.php?as=%%%id%%%");return false;', true);
    $obj_list->add_column('page_edit', '', '', '', $_SERVER['PHP_SELF'] . '?task=edit&editto=%%%id%%%', '', false);
    $obj_list->add_column('page_delete', '', '', '', '#', 'return askFirstBeforeDelete("%%%id%%%")', false);

    $obj_list->init_list($_SERVER['PHP_SELF']);

    include ABSTRACT_PRIVATE_HEADER;
    //include 'abstract_subnav.inc';
    echo '<div style="text-align:right;width:95%;">
          <input type="button" value="Add New Url" onclick="location.href=\'?task=add\'"/>
        </div><br>';
    echo '<div class="gray_box" style="">';
    echo '<strong>Extracted Page List:</strong><br/>';
    echo "<span style='color: #ff0000'>$msg</span><br />";
    echo $obj_list->get_html();
    echo '</div>';

    include ABSTRACT_PRIVATE_FOOTER;
    break;

}
?>
<script>
function askFirstBeforeDelete(id)
{
  if(window.confirm("Are you sure you want to remove this page")){
    document.location.href = "?task=delete&del_id="+id;
    return true;
  }
  return false;
}
function popwin5(url) {
  win = window.open(url,"","menubar=no,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=no");
  // This probably only works in firefox
  //win.onblur = function() {win.close();}
  // Dan didt like this effect for the reports anyway
}
</script>